package com.example.demo31;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.demo31.fragment.electFragment;
import com.example.demo31.fragment.interfaceFragment;
import com.example.demo31.fragment.screenFragment;
import com.example.demo31.fragment.softpanFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class WechatActivity extends AppCompatActivity {

    List<Fragment> list; //创建装载多个fragment的列表

    BottomNavigationView bottomnavigation;
    

    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_wechat);

         bottomnavigation = findViewById(R.id.bottonnavigate);

        list = new ArrayList<>();
        list.add(new screenFragment());
        list.add(new electFragment());
        list.add(new interfaceFragment());
        list.add(new softpanFragment());

        showfragment(list.get(0)); //默认首页




        //切换事件
        bottomnavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                int itemId = item.getItemId();

                // 根据菜单项ID切换对应Fragment
                if (itemId == R.id.menu_message) { // 假设第一个菜单项ID
                    showfragment(list.get(0));
                    return true;
                } else if (itemId == R.id.menu_book) { // 第二个菜单项ID
                    showfragment(list.get(1));
                    return true;
                } else if (itemId == R.id.menu_dis) { // 第三个菜单项ID
                    showfragment(list.get(2));
                    return true;
                } else if (itemId == R.id.menu_wp) { // 第四个菜单项ID
                    showfragment(list.get(3));
                    return true;
                }
                return false;
            }
        });


        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    public void tomain(View view)
    {
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);

    }

    public void showfragment(Fragment fragment)
    {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container,fragment);
        ft.commit();

    }
}